package com.example.hzq.food.activity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hzq.food.R;
import com.example.hzq.food.bean.BuyBean;
import com.example.hzq.food.bean.CollectBean;
import com.example.hzq.food.fragment.ShopFoodfragment;
import com.example.hzq.food.fragment.ShopScorefragment;
import com.example.hzq.food.model.Model;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.food.activity.Login.userid;
import static com.example.hzq.food.adapter.Shopadapter.imagepath;
import static com.example.hzq.food.adapter.Shopadapter.shop_id;
import static com.example.hzq.food.adapter.Shopadapter.shopaddress;
import static com.example.hzq.food.adapter.Shopadapter.shopintro;
import static com.example.hzq.food.adapter.Shopadapter.shopname;
import static com.example.hzq.food.adapter.Shopadapter.shoppn;

/**
 * Created by hzq on 2017/6/12.
 */

public class Shop extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout layout1,layout_food,layout_score;

    private ImageButton ib1,ib2;
    private Button b1,b2,tobuy;
    private TextView tv_shopname,tv_shoppn,tv_shopaddress,tv_shopintro,foodnum,foodsum;
    private ImageView shopimage;
    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;
    private ViewPager viewPager;
    private boolean collectflag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shop);
        init();
        initevent();
        initfragment();
        judge();
    }
    private void change(){
        int id=Integer.valueOf(userid);
        Model model=new Model();
        Call<CollectBean>call=model.collectornoshop(id,shop_id);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.body().getSuccess().toString().equals("1")){
                    if (collectflag){
                        ib2.setImageResource(R.drawable.ic_collect);
                        collectflag=false;
                    }
                    else {
                        ib2.setImageResource(R.drawable.ic_collect1);
                        collectflag=true;
                    }
                }
            }
            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {

            }
        });
    }
    private void judge(){
        final int id=Integer.valueOf(userid);
        Model model=new Model();
        Call<CollectBean>call=model.iscollect(id,shop_id,0);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.isSuccessful()&&response!=null){
                        if (response.body().getCollected().toString().equals("1")){
                            ib2.setImageResource(R.drawable.ic_collect1);
                            collectflag=true;
                        }
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {

            }
        });
    }
    private void init(){
        layout1 = (LinearLayout) findViewById(R.id.linearlayout5);
        layout_food = (LinearLayout) findViewById(R.id.linearlayout9);
        layout_score = (LinearLayout) findViewById(R.id.linearlayout10);
        tv_shopname = (TextView) findViewById(R.id.textView21);
        tv_shoppn = (TextView) findViewById(R.id.textView24);
        tv_shopintro = (TextView) findViewById(R.id.textView22);
        tv_shopaddress = (TextView) findViewById(R.id.textView23);
        shopimage = (ImageView) findViewById(R.id.imageview2);
        ib1 = (ImageButton) findViewById(R.id.imagebutton5);
        ib2 = (ImageButton) findViewById(R.id.imagebutton6);
        viewPager=(ViewPager)findViewById(R.id.viewpage1);
        b1=(Button)findViewById(R.id.button5);
        b2=(Button)findViewById(R.id.button6);
        tv_shopname.setText(shopname);
        tv_shoppn.setText(shoppn);
        tv_shopaddress.setText(shopaddress);
        tv_shopintro.setText(shopintro);
        Picasso.with(this).load(imagepath).into(shopimage);

    }
    private void initevent(){
        layout1.setOnClickListener(this);
        layout_food.setOnClickListener(this);
        layout_score.setOnClickListener(this);
        ib1.setOnClickListener(this);
        ib2.setOnClickListener(this);
    }
    private void initfragment(){
        listfragment = new ArrayList<Fragment>();
        listfragment.add(new ShopFoodfragment());
        listfragment.add(new ShopScorefragment());
        fpadapter= new  FragmentPagerAdapter(getSupportFragmentManager())  {
            @Override
            public int getCount() {
                return listfragment.size();
            }

            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }
        };
        viewPager.setAdapter(fpadapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
               viewPager.setCurrentItem(position);
                reset();
                selectTab(position);

            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private  void reset()
    {
       b1.setTextColor(getResources().getColor(R.color.black));
       b2.setTextColor(getResources().getColor(R.color.black));

    }
    private void selectTab(int i)
    {
        switch (i)
        {
            case 0:
                b1.setTextColor(getResources().getColor(R.color.colorChecked));
                break;
            case 1:
                b2.setTextColor(getResources().getColor(R.color.colorChecked));
                break;
            default:
            break;
        }
        viewPager.setCurrentItem(i);
    }
    @Override
    public void onClick(View v) {
            switch (v.getId()){
                case R.id.linearlayout5:
                    Toast.makeText(Shop.this,"要显示店铺详情和评价",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.linearlayout9:
                   reset();
                    selectTab(0);
                    break;
                case R.id.linearlayout10:
                    reset();
                    selectTab(1);
                    break;
                case R.id.imagebutton5:
                    finish();
                    break;
                case R.id.imagebutton6:
                    change();
                default:
                    break;
            }
    }

}
